package fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import bean.MainBean;
import qf.wlh.com.oneprisdent.R;
import qf.wlh.com.oneprisdent.adapter.MainViewPagerAdapter;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import utils.RetrofitMainFragmentInterface;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends Fragment {
    private Retrofit retrofit;
    private MainViewPagerAdapter adapter;
    private ViewPager viewPager;
    private FragmentManager fragmentManager;

    public MainFragment() {
        // Required empty public constructor

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_main, container, false);
        viewPager = ((ViewPager) view.findViewById(R.id.main_viewpager));

        fragmentManager=getFragmentManager();
        retrofit=new Retrofit.Builder()
                .baseUrl("http://api.yixi.tv/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RetrofitMainFragmentInterface retrofitMainFragmentInterface=retrofit
                .create(RetrofitMainFragmentInterface.class);
        retrofitMainFragmentInterface.getData().enqueue(new Callback<MainBean>() {
            @Override
            public void onResponse(Call<MainBean> call, Response<MainBean> response) {
                List<MainBean.DataBean> data = response.body().getData();
                adapter=new MainViewPagerAdapter(data,getActivity(),fragmentManager);
                viewPager.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<MainBean> call, Throwable t) {

            }
        });
        return view;
    }

}
